package com.smarthome.web.property.controller.account;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.account.AccountFeatureService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShAccountFeature;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.Tree;

@Controller
@RequestMapping(value="/accountFeature")
public class AccountFeatureController {
	
	@Resource
	private AccountFeatureService accountFeatureService;
	
	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree(){
		
		return accountFeatureService.tree(null); 
	}
	
	@RequestMapping("/treeGrid")
	@ResponseBody
	public List<ShAccountFeature> treeGrid(ShAccount account){
		List<ShAccountFeature> res=accountFeatureService.treeGrid(null);
		return res;
	}
	
	@RequestMapping("/addPage")
	public String addPage(){
		return "/account/accountFeatureAdd";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShAccountFeature accountFeature){
		accountFeature.setActive(1);
		accountFeatureService.add(accountFeature);
		return Result.success("添加成功！");
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request,Long id){
		ShAccountFeature accountFeature=accountFeatureService.queryAccountFeatureById(id);
		request.setAttribute("feature", accountFeature);
		return "/account/accountFeatureEdit";
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(ShAccountFeature accountFeature){
		accountFeatureService.edit(accountFeature);
		return Result.success("更改成功！");
	}
	@RequestMapping("/delete")
	@ResponseBody
	public Result delete(Long id){
		ShAccountFeature feature = new ShAccountFeature();
		feature.setParentId(id);
		feature.setActive(1);//正常未删除的功能
		List<ShAccountFeature> features = accountFeatureService.queryFeaturesByCondition(feature);
		if(null != features && features.size()>0){
			return Result.success("删除失败，该功能被子功能引用！");
		}
		ShAccountFeature accountFeature = accountFeatureService.queryAccountFeatureById(id);
		accountFeature.setActive(0);
		accountFeatureService.edit(accountFeature);
		return Result.success("删除成功");
	}
	
}
